﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;

namespace ZapHelper
{
    public class JavaScriptCategoryParser : CategoryParser
    {

        private static String TYPE_ATTRIBUTE = "type";

        protected override IEnumerable<Category> ParseCaterories()
        {
            var document = this.Document;

            // Select All of the <script> nodes.
            var nodes =
                document.DocumentNode.SelectNodes("//script");

            if (nodes == null)
            {
                throw new ParseFailedException("Cant parse this uri using this method.");
            }


            foreach (var node in nodes)
            {
                // Check that the current node is a relevant node
                // TODO: figure the rules

                var hrefAttribute = node.Attributes.Where(x => x.Name.Equals(TYPE_ATTRIBUTE, StringComparison.CurrentCultureIgnoreCase)).SingleOrDefault();
                var nodeValue = node.InnerText;

                //if (hrefAttribute == null || String.IsNullOrWhiteSpace(nodeValue))
                //{
                //    continue;
                //}

                if (String.IsNullOrWhiteSpace(nodeValue))
                {
                    continue;
                }

                var delimiter = new string[] { ".add(" };

                var comma = nodeValue.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);

                foreach (var item1 in comma)
                {

                    var split2 = item1.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);

                    if (split2.Length < 4)
                    {
                        continue;
                    }

                    // TODO use utils.trim" 
                    var categoryName = 
                        split2[2];

                    var categoryUrl = 
                        split2[3].Split(new string[] { ");" }, StringSplitOptions.RemoveEmptyEntries)
                        .First();

                    if (String.IsNullOrWhiteSpace(categoryName) || String.IsNullOrWhiteSpace(categoryUrl))
                    {
                        continue;
                    }

                    // Trim vars
                    categoryName =
                        Utils.TrimQuoteMarksAndDecodeHtml(categoryName);

                    categoryUrl =
                        Utils.TrimQuoteMarksAndDecodeHtml(categoryUrl);

                    // Recheck after trimming
                    if (String.IsNullOrWhiteSpace(categoryName) || String.IsNullOrWhiteSpace(categoryUrl))
                    {
                        continue;
                    }

                    Uri uri;
                    try
                    {
                        uri = 
                            Utils.CreateReferenceUri(this.CategoriesUri, categoryUrl);
                    }
                    catch
                    {
                    	// Ignored purposely   
                        continue;
                    }

                    var category = new Category()
                    {
                        Name = categoryName,
                        ProductsUri = uri
                    };

                    yield return category;

                }
            }
        
        }
    }
}
